package com.water_work_form.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.water_work_form.entity.RecordSpectrophotometric;
import com.water_work_form.mapper.*;
import com.water_work_form.service.RecordSpectrophotometricService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 记录——分光光度法检测铝、铁、氨、亚硝酸盐原始记录 服务实现类
 * </p>
 *
 * @author xuke
 * @since 2023-09-14
 */
@Service
public class RecordSpectrophotometricServiceImpl extends ServiceImpl<RecordSpectrophotometricMapper, RecordSpectrophotometric> implements RecordSpectrophotometricService {

    @Autowired
    RecordCapacityAnalysisMapper recordCapacityAnalysisMapper;

    @Autowired
    BasicFormMapper basicFormMapper;

    @Autowired
    BasicRestrictionMapper basicRestrictionMapper;

    @Autowired
    BasicWaterMapper basicWaterMapper;

    @Autowired
    BasicCurveMapper basicCurveMapper;

    @Autowired
    BasicCurvePreparationMapper basicCurvePreparationMapper;

    @Autowired
    BasicDeviceMapper basicDeviceMapper;

    @Autowired
    BasicPhMapper basicPhMapper;

    @Autowired
    BasicFormulaMapper basicFormulaMapper;

    @Autowired
    BasicVolumeMapper basicVolumeMapper;

    @Autowired
    BasicDrugMapper basicDrugMapper;



    /**
     * 记录——分光光度法检测铝、铁、氨、亚硝酸盐原始记录 项目查询（两个查询项+分页+排序）
     * @param checkSite 检测地点（check_site） 若为空则不进行此项查询，否则返回模糊查询后的数据
     * @param checkStartDate 检测日期（check_date） 范围查询开始日期，若为空则不进行此项查询，否则返回check_date大于checkStartDate的数据
     * @param checkEndDate 检测日期（check_date） 范围查询结束日期，若为空则不进行此项查询，否则返回check_date小于checkEndDate的数据
     * @param status 项目状态（status） 若为空则不进行此项查询，否则返回对应数据
     * @param current 分页查询 当前页数
     * @param size 分页查询 每页最大数据量
     * @return 返回符合以上查询条件的数据 并按照 状态（status）、领样日期（receive_date）、检测日期（check_date）的先后顺序作为优先级（即状态status为优先级最高，进行首先排序）进行升序排序
     */
    @Override
    public IPage<RecordSpectrophotometric> selectRecordSpectrophotometric(String checkStartDate, String checkEndDate, String checkSite, String status, Integer current, Integer size){
        QueryWrapper<RecordSpectrophotometric> queryWrapper = new QueryWrapper<>();

        if( checkStartDate != null && checkStartDate.length() > 0){
            queryWrapper.ge("check_date", checkStartDate);
        }
        if( checkEndDate != null && checkEndDate.length() > 0){
            queryWrapper.le("check_date", checkEndDate);
        }
        if( checkSite != null && checkSite.length() > 0 ){
            queryWrapper.like("check_site", checkSite);
        }
        if( status != null && status.length() > 0 ){
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByAsc("status", "receive_date", "check_date");
        IPage<RecordSpectrophotometric> page = new Page<>(current, size);
        return this.page(page,queryWrapper);
    }

    /**
     * 记录——分光光度法检测铝、铁、氨、亚硝酸盐原始记录 基础表数据查询
     * @param formName form表name查询项
     * @param type device表name查询项
     * @param name1 多表查询项 基础表restriction、curve、curve_preparation、formula、drug的name列查询项（除drug表外查询到的都是对应项，而drug表查询所有符合的数据列表）
     * @param name2 多表查询项 基础表restriction、curve、curve_preparation、formula、drug的name列查询项（除drug表外查询到的都是对应项，而drug表查询所有符合的数据列表）
     * @param name3 多表查询项 基础表restriction、curve、curve_preparation、formula、drug的name列查询项（除drug表外查询到的都是对应项，而drug表查询所有符合的数据列表）
     * @param name4 多表查询项 基础表restriction、curve、curve_preparation、formula、drug的name列查询项（除drug表外查询到的都是对应项，而drug表查询所有符合的数据列表）
     * @param name5 restriction表name列查询项
     * @param waterId water表id列查询项
     * @param volume volume表name列查询项
     * @return 返回所有符合的数据项，仅有一个查询项的表以表名作为键，大于一个查询项的键名对应查询项数字
     */
    @Override
    public Map<String,Object> selectBasicSpectrophotometric(String formName, String type, String name1, String name2, String name3, String name4,String name5, String waterId,
                                                            String volume) {
        Map<String,Object> map = new HashMap<>();
        map.put("form", basicFormMapper.getOneByName(formName));
        map.put("restriction1", basicRestrictionMapper.getOneByName(name1));
        map.put("restriction2", basicRestrictionMapper.getOneByName(name2));
        map.put("restriction3", basicRestrictionMapper.getOneByName(name3));
        map.put("restriction4", basicRestrictionMapper.getOneByName(name4));
        map.put("restriction5", basicRestrictionMapper.getOneByName(name5));
        map.put("water", basicWaterMapper.getOneByName(waterId));
        map.put("curve1", basicCurveMapper.getOneByName(name1));
        map.put("curve2", basicCurveMapper.getOneByName(name2));
        map.put("curve3", basicCurveMapper.getOneByName(name3));
        map.put("curve4", basicCurveMapper.getOneByName(name4));
        map.put("curvePreparation1", basicCurvePreparationMapper.getOneByName(name1));
        map.put("curvePreparation2", basicCurvePreparationMapper.getOneByName(name2));
        map.put("curvePreparation3", basicCurvePreparationMapper.getOneByName(name3));
        map.put("curvePreparation4", basicCurvePreparationMapper.getOneByName(name4));
        map.put("formula1", basicFormulaMapper.getOneByName(name1));
        map.put("formula2", basicFormulaMapper.getOneByName(name2));
        map.put("formula3", basicFormulaMapper.getOneByName(name3));
        map.put("formula4", basicFormulaMapper.getOneByName(name4));
        map.put("volume", basicVolumeMapper.listOneByName(volume));
        map.put("drug1", basicDrugMapper.listByName(name1));
        map.put("drug2", basicDrugMapper.listByName(name2));
        map.put("drug3", basicDrugMapper.listByName(name3));
        map.put("drug4", basicDrugMapper.listByName(name4));
        map.put("device", basicDeviceMapper.getOneByName(type));
        return map;
    }

    /**
     * 记录——分光光度法检测铝、铁、氨、亚硝酸盐原始记录 数据表导出
     * @param recordSpectrophotometric 需要导出的数据表
     * @throws IOException 异常
     */
    @Override
    public void downloadRecordSpectrophotometric(RecordSpectrophotometric recordSpectrophotometric) throws IOException {

        Map<String, Object> data = new HashMap<String, Object>();
        //模板地址
        String templateFilePath = "E:\\wyf\\doxFiles\\";
        //生成文件的保存地址
        String destFilePath = "E:\\wyf\\doxFiles\\outFile\\";

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        JSONObject preparationCheckValue =  JSON.parseObject(recordSpectrophotometric.getPreparationCheckValue());
        JSONObject waterCheckValue =  JSON.parseObject(recordSpectrophotometric.getWaterCheckValue());

        data.put("receiveDate", dateFormat.format(recordSpectrophotometric.getReceiveDate()));
        data.put("checkDate", dateFormat.format(recordSpectrophotometric.getCheckDate()));
        data.put("preparationCheckValue", preparationCheckValue);
        data.put("waterCheckValue", waterCheckValue);
        data.put("list", recordSpectrophotometric);

        //渲染文件
        XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "RecordSpectrophotometric.docx");
        compile.render(data);
        //输出为文件，指定输出文件名
        compile.writeToFile(destFilePath + recordSpectrophotometric.getId() + "out_recordSpectrophotometric.docx");
        compile.close();
    }



}
